package cn.czh.org.controller;

import cn.czh.basic.util.AjaxResult;
import cn.czh.basic.util.PageList;
import cn.czh.org.domain.Employee;
import cn.czh.org.query.EmployeeQuery;
import cn.czh.org.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 员工接口类
 */
@RestController
@RequestMapping("/employee")
@Api(value = "员工接口类",description = "员工的CRUD操作")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;

    /**
     * 接口:查询所有
     * @return
     */
    //http://localhost:8080/employee - Get
    @GetMapping
    @ApiOperation(value = "查询所有接口" )
    public List<Employee> findAll(){
        return employeeService.findAll();
    }

    /**
     * 接口:查询单个
     * @param id
     * @return
     */
    //http://localhost:8080/employee/324 - Get
    @GetMapping("/{id}")
    @ApiOperation(value = "查询单个接口")
    public Employee findOne(@PathVariable("id") Long id){
        return employeeService.findOne(id);
    }

    /**
     * 接口：分页查询
     * @param employeeQuery
     * @return
     */
    //http://localhost:8080/employee - Post 参数通过json对象传递
    @PostMapping
    @ApiOperation(value = "分页查询接口")
    //@RequestBody - 从请求体中获取数据并赋值给employeeQuery
    public PageList<Employee> queryPage(@RequestBody EmployeeQuery employeeQuery){
        return employeeService.queryPage(employeeQuery);
    }

    /**
     * 接口：删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除接口")
    public AjaxResult del(@PathVariable("id") Long id){
        try {
            employeeService.del(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            //return new AjaxResult(false,"删除成功");
            //链式语法
            return AjaxResult.me().setSuccess(false).setMsg("删除失败").setResultObj("请重新操作");
        }
    }

    /**
     * 接口：批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    @ApiOperation(value = "批量删除接口")
    public AjaxResult patchDel(@RequestBody List<Long> ids){
        try {
            employeeService.patchDel(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            //return new AjaxResult(false,"删除成功");
            //链式语法
            return AjaxResult.me().setSuccess(false).setMsg("删除失败").setResultObj("请重新操作");
        }
    }

    @PutMapping
    @ApiOperation(value = "添加或者修改接口")
    //@RequestBody - 从请求体中获取数据并赋值给employee
    public AjaxResult addOrUpdate(@RequestBody Employee employee){
        try {
            //如果没有获取id就是添加
            if (employee.getId() == null){
                employeeService.add(employee);
            }else {
                //否则就是修改
                employeeService.update(employee);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            //return new AjaxResult(false,"操作失败");
            //链式语法
            return AjaxResult.me().setSuccess(false).setMsg("操作失败").setResultObj("请重新操作");
        }
    }
}
